package org.thread.test06.ts.demo05.t01;

import java.util.LinkedList;

/**
 * 篮子 - 装水果
 *
 * @author CSY
 * @date 2025 - 03 - 14
 */
public class Box {
    /**
     * 篮子容器
     */
    private LinkedList<Fruit> boxList = new LinkedList<>();

    /**
     * 最大容量
     */
    private static final int MAX_SIZE = 5;

    /**
     * 放水果
     *
     * @param name 谁？
     * @param f    什么水果
     */
    public synchronized void put(String name, Fruit f) {
        if (boxList.size() == MAX_SIZE) {
            this.notify();
            System.out.println("呼叫女孩吃水果...");
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.printf("%s摘了一个%s放入篮子%n", name, f);
        boxList.add(f);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 取水果
     *
     * @param name 谁
     */
    public synchronized void take(String name) {

        if (boxList.isEmpty()) {
            this.notify();
            System.out.println("呼叫男孩摘水果...");
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
//        System.out.printf("%s吃了一个%s %n", name,boxList.getLast());

            Fruit delFruit = boxList.removeLast();
            System.out.printf("%s吃了一个%s %n", name,delFruit);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
